
package com.magicalcoder.youyamvc.app.goods.service.impl;

import com.magicalcoder.youyamvc.app.goods.dao.GoodsDao;
import com.magicalcoder.youyamvc.app.goods.service.GoodsService;
import com.magicalcoder.youyamvc.app.model.Goods;
import com.magicalcoder.youyamvc.core.common.utils.ListUtils;
import com.magicalcoder.youyamvc.core.common.utils.ProjectUtil;
import com.magicalcoder.youyamvc.core.common.utils.StringUtils;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.magicalcoder.youyamvc.core.common.utils.copy.CopyerSpringUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
*  欢迎加入官方QQ群:323237052
*/

@Component("goodsService")
public class GoodsServiceImpl implements GoodsService{
    @Resource(name="goodsDao")
    private GoodsDao goodsDao;


    @Override
    public Goods getGoods(Long id) {
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("id", id);
        return goodsDao.getGoods(query);
    }

    @Override
    public Goods selectOneGoodsWillThrowException(Map<String, Object> query){
        return goodsDao.getGoods(query);
    }

    @Override
    public List<Goods> getGoodsList(Map<String, Object> query) {
        return goodsDao.getGoodsList(query);
    }

    @Override
    public Integer getGoodsListCount(Map<String, Object> query) {
        return goodsDao.getGoodsListCount(query);
    }

    @Override
    public     Long  insertGoods(Goods entity) {
        return goodsDao.insertGoods(entity);
    }

    @Override
    public void updateGoods(Goods entity) {
        //校验
        goodsDao.updateGoods(entity);
    }

    @Override
    public void updateGoodsWithoutNull(Goods entity) {
        //校验
        goodsDao.updateGoodsWithoutNull(entity);
    }

    @Override
    public void updateGoodsByWhereSql(Map<String,Object> entity,String whereSql) {
        if(entity==null ||entity.isEmpty()){
            throw new RuntimeException("entity不能为空");
        }
        if(StringUtils.isBlank(whereSql)){
            throw new RuntimeException("whereSql不能为空");
        }
        entity.put("whereSql",whereSql);
        //校验
        goodsDao.updateGoodsByWhereSql(entity);
    }

    @Override
    public void deleteGoods(Long id) {
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("id", id);
        goodsDao.deleteGoods(query);
    }
    @Override
    public void deleteGoodsList(Map<String,Object> entity){
        goodsDao.deleteGoodsList(entity);
    }

    @Override
    public void deleteGoodsByWhereSql(String whereSql) {
        if(StringUtils.isBlank(whereSql)){
            throw new RuntimeException("whereSql不能为空");
        }
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("whereSql", whereSql);
        goodsDao.deleteGoodsByWhereSql(query);
    }


    @Override
    public void truncateGoods() {
        goodsDao.truncateGoods();
    }

    @Override
    public void batchInsertGoods(List<Goods> list) {
        //校验
        goodsDao.batchInsertGoods(list);
    }

    @Override
    public void batchUpdateGoods(List<Goods> list) {
        //校验
        goodsDao.batchUpdateGoods(list);
    }
    @Override
    public void batchDeleteGoods(List<Long> idList) {
        goodsDao.batchDeleteGoods(idList);
    }

    @Override
    public void batchDeleteGoodsList(List<Goods> entityList){
        goodsDao.batchDeleteGoodsList(entityList);
    }

    @Transactional
    @Override
    public void transactionImportJsonList(List<Goods> list) {
        if(list!=null && list.size()>0){
            for(Goods goods : list){
                if (goods.getId() == null) {
                    insertGoods(goods);
                } else {
                    Goods entity = getGoods(goods.getId());
                    if(entity==null){
                        insertGoods(goods);
                    }else {
                        CopyerSpringUtil.copyProperties(goods, entity);
                        updateGoods(entity);
                    }
                }
            }
        }
    }


    public List<Goods> tryQueryList(String keyword,String selectValue, String foreignJavaField){
        List<Goods> list;
        if(StringUtils.isBlank(keyword) || "全部".equals(keyword)){
            return getGoodsList(ProjectUtil.buildMap("limitIndex",0,"limit", 20));
        }else{
            list = searchList("goodsNameFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            list = searchList("createTimeFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            list = searchList("publisStatusFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
        }
        return new ArrayList<Goods>();
    }

    private List<Goods> searchList(String field,String keyword){
        List<Goods> list = getGoodsList(ProjectUtil.buildMap(field,keyword,"limitIndex",0,"limit", 20));
        if(ListUtils.isNotBlank(list)){
            return list;
        }
        String[] keys = keyword.split("-");
        for(String key:keys){
            list = getGoodsList(ProjectUtil.buildMap(field,key,"limitIndex",0,"limit", 20));
            if(ListUtils.isNotBlank(list)){
                return list;
            }
        }
        return null;
    }
}
